package stsj.meddroid.screen;

import stsj.meddroid.MainAppListener;
import stsj.meddroid.Resources;
import stsj.meddroid.Settings;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.FadeIn;
import com.badlogic.gdx.scenes.scene2d.actions.FadeOut;
import com.badlogic.gdx.scenes.scene2d.actions.Parallel;
import com.badlogic.gdx.scenes.scene2d.actions.Sequence;
import com.badlogic.gdx.scenes.scene2d.actors.Image;

public class SplashScreen implements Screen {

	private final static int ZERO = -1;
	private final static int FIRST = 0;
	private final static int SECOND = 1;
	private final static int END = 2;

	private MainAppListener mainListener;

	private  Stage stage;
	private  SpriteBatch batch  ;
	private  Texture productionLogo;
	private  Texture compfest2011Logo;
	private  Texture loading;
	private  Texture loadingText;

	private final Image productionImage ;
	private final Image compfestImage;
	private final Image loadingImage;
	// private Animation loadingAnim ;

	private int state;

	private boolean initStart = false;
	private int initCounter = 0;

	private long countTime;
	private long startTime;

	private final TextureRegion[] kataLoadings;
	
	private Camera camera;

	public SplashScreen(MainAppListener mainListener) {
		this.mainListener = mainListener;
		Gdx.input.setCatchBackKey(false);
		Gdx.app.log("new Splash Screen", "new");
		camera = Resources.camera;
		
		
		stage = new Stage(480, 320, true);
		
		batch = Resources.batch;

		// Loading 
		productionLogo = new Texture(Gdx.files.internal("dll/splashlogo.png"));
		compfest2011Logo = new Texture(Gdx.files.internal("dll/splashcompfest.png"));
		loading = new Texture(Gdx.files.internal("dll/androidloading.png"));
		loadingText = new Texture(Gdx.files.internal("dll/kataloading.png"));
		
		kataLoadings = new TextureRegion[4];
		kataLoadings[0] = new TextureRegion(loadingText, 0, 0, 75, 20);
		kataLoadings[1] = new TextureRegion(loadingText, 75, 0, 75, 20);
		kataLoadings[2] = new TextureRegion(loadingText, 150, 0, 75, 20);
		kataLoadings[3] = new TextureRegion(loadingText, 225, 0, 75, 20);

		Resources.kataLoadings = kataLoadings;

		productionImage = new Image("Our Production",  new TextureRegion(productionLogo, 0, 0, 480, 320)); // logo septu
																	// n ton sam
																	// jong
		compfestImage = new Image("Compfest 2011", new TextureRegion(compfest2011Logo, 0, 0, 480, 320)); // logo compfest
		loadingImage = new Image("Loading", new TextureRegion(loading, 0, 0,
				128, 128)); // gambar loading
		loadingImage.x = stage.width() / 2 - 64; // biar ditengah2
		loadingImage.y = stage.height() / 2 - 64;
	}

	@Override
	public void dispose() {
//		Gdx.app.log("Splash screen dispose", "===================");
		productionLogo.dispose();
		compfest2011Logo.dispose();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Splash screen hide", "===================");
		stage.dispose();
		if (Settings.soundEnabled) {
			Resources.bgmOpening.play(); // firstplay for opening
		}
		dispose();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Splash screen pause", "===================");
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		countTime = System.currentTimeMillis();
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30.0f));
		stage.draw();
		
		camera.update();
		batch.setProjectionMatrix(camera.combined);	
		
		switch (state) {
		case ZERO: {
//			Gdx.app.log("Splash", "Zero");
			
			state = FIRST;
			stage.addActor(productionImage);
			productionImage.action(Parallel.$(Sequence.$(FadeIn.$(1f), Delay.$(FadeOut
					.$(1f), 2f))));
//			Gdx.app.log("Splash", "Zero end");
			
			break;
		}
		case FIRST: {
			if ((countTime - startTime) >= 5000) {
//				Gdx.app.log("Splash", "first");
				
				stage.removeActor(productionImage);
				stage.addActor(compfestImage);
				compfestImage.action(Parallel.$(Sequence.$(FadeIn.$(1f), Delay.$(FadeOut
						.$(1f), 2f))));
				state = SECOND;
//				Gdx.app.log("Splash", "first end");
				
			}
			break;
		}
		case SECOND: {
			if ((countTime - startTime) >= 9000) {
				stage.removeActor(compfestImage);
				stage.addActor(loadingImage);
				state = END;
			}
			break;
		}
		case END: {
			Resources.batch.begin();
			Resources.batch.draw(kataLoadings[0], 480
					- kataLoadings[0].getRegionWidth(), 0);
			Resources.batch.end();
			if ((countTime - startTime) >= 12000) {
//				Gdx.app.log("Render", "Saat loading..");

				if (!initStart) {
					Gdx.app.log("Set to loading", "YES");
					switch (initCounter) {
					case 0: {
						Resources.batch.begin();
						Resources.batch.draw(kataLoadings[0], 480
								- kataLoadings[0].getRegionWidth(), 0);
						Resources.batch.end();
						Resources.initMenu();
						initCounter++;
						break;
					}
					case 1: {
						Resources.batch.begin();

						Resources.batch.draw(kataLoadings[1], 480
								- kataLoadings[1].getRegionWidth(), 0);
						Resources.batch.end();
						Resources.initStages();
						initCounter++;
						break;
					}
					case 2: {
						Resources.batch.begin();

						Resources.batch.draw(kataLoadings[2],480
								- kataLoadings[2].getRegionWidth(), 0);
						Resources.batch.end();
						Resources.initBattleTextures();
						initCounter++;
						break;
					}
					case 3: {
						Resources.batch.begin();

						Resources.batch.draw(kataLoadings[3], 480
								- kataLoadings[3].getRegionWidth(), 0);
						Resources.batch.end();
						Resources.initSounds();
						initCounter++;
						initStart = true;
						break;
					}

					}
				}
				if (Resources.finish) {
//					Gdx.app.log("Set to Main Menu", "YES");
					mainListener.initializeAllScreens();
					mainListener.setState(MainAppListener.MAIN_MENU);
				}
			}
			break;
		}
		default:
			break;
		}

		
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
//		Gdx.app.log("Splash screen resize", "===================");
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Splash screen resume", "===================");
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Splash screen show", "===================");
		state = ZERO;
		startTime = System.currentTimeMillis();
		camera.position.set(Resources.VP_WIDTH/2, Resources.VP_HEIGHT/2, 0);
	}
}
